package com.asj.house.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.asj.house.constant.RedisCacheTime;
import com.asj.house.constant.RedisConstants;
import com.asj.house.entity.HouseMenu;
import com.asj.house.mapper.HouseMenuMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.service.HouseMenuService;
import com.asj.house.utils.RedisUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-16
 */
@Service
@Slf4j
public class HouseMenuServiceImpl extends ServiceImpl<HouseMenuMapper, HouseMenu> implements HouseMenuService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public RespEntity<List<HouseMenu>> queryAllMenus() {

        String value = redisUtil.get(RedisConstants.key.MENU);

        if (StringUtils.isBlank(value)) {
            List<HouseMenu> menuList = baseMapper.queryAllMenus();
            String menu = JSONUtil.toJsonStr(menuList);
            redisUtil.set(RedisConstants.key.MENU, menu, RedisCacheTime.MENU_TIME);
            return new RespEntity(RespCode.SUCCESS, menuList);
        }

        JSONArray jsonArray = JSONUtil.parseArray(value);
        List<HouseMenu> menuList = JSONUtil.toList(jsonArray, HouseMenu.class);
        return new RespEntity<>(RespCode.SUCCESS, menuList);
    }
}
